package edu.whu.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import edu.whu.demo.domain.Schedule;
import edu.whu.demo.dao.ScheduleDao;
import edu.whu.demo.service.IScheduleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2022-11-24
 */
@Service
public class ScheduleServiceImpl extends ServiceImpl<ScheduleDao, Schedule> implements IScheduleService {

    @Override
    public Schedule addSchedule(Schedule schedule) {
        try{
            Integer id =  this.baseMapper.insert(schedule);
            return this.baseMapper.selectById(id);
        }catch (Exception ex){
            return null ;
        }
    }

    @Override
    public Schedule getScheduleById(Long id) {
        return this.baseMapper.selectById(id);
    }

    //有两种情况：用户自行更改日程，活动日程的变更
    //一般用户不可以更改活动日程的title、beginTime、type、location
    @Override
    public Schedule updateScheduleUser(Long id, Schedule schedule) {
        if(this.baseMapper.selectById(schedule.getId())==null||id!=schedule.getId()){
            /*对应日程不存在*/
            return null;
        }else{
            if(schedule.getType().equals("活动日程")){
                LambdaUpdateWrapper<Schedule> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                lambdaUpdateWrapper.eq(Schedule::getUid, schedule.getUid())
                        .set(Schedule::getEndTime, schedule.getEndTime())
                        .set(Schedule::getRemind, schedule.getRemind())
                        .set(Schedule::getFrequency, schedule.getFrequency())
                        .set(Schedule::getRepeatEndTime,schedule.getRepeatEndTime());
                this.baseMapper.update(null, lambdaUpdateWrapper);
                return this.baseMapper.selectById(id);
            }
            try {
                this.baseMapper.updateById(schedule);
                return this.baseMapper.selectById(id);
            }catch (Exception ex){
                return null;
            }
        }
    }

    /*只允许更改name\place\time*/
    @Override
    public Schedule updateScheduleActivity(Long id, Schedule schedule) {
        if(this.baseMapper.selectById(schedule.getId())==null||id!=schedule.getId()){
            /*对应日程不存在*/
        }
        try{
            if(schedule.getType().equals("活动日程")){
                LambdaUpdateWrapper<Schedule> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                lambdaUpdateWrapper.eq(Schedule::getTitle, schedule.getUid())
                        .set(Schedule::getBeginTime, schedule.getEndTime())
                        .set(Schedule::getLocation, schedule.getRemind());
                this.baseMapper.update(null, lambdaUpdateWrapper);
                return this.baseMapper.selectById(id);
            }
            return null;
        }catch (Exception ex){
            return null;
        }
    }

    @Override
    public Schedule updateSchedule(Long id, Schedule schedule) {
        if(this.baseMapper.selectById(schedule.getId())==null||id!=schedule.getId()){
            /*对应日程不存在*/
        }try{
            this.baseMapper.updateById(schedule);
            return this.baseMapper.selectById(id);
        }catch (Exception ex){
            return null;
        }
    }

    @Override
    public void deleteSchedule(Long id) {
        this.baseMapper.deleteById(id);
    }

    @Override
    public IPage<Schedule> findSchedule(Long uid, String searchInfo, Long pageNum, Long pageSize) {
        return null;
    }
}
